package com.kunbo.cn.app.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.kunbo.cn.app.dao.DeliveryNoticeDao;
import com.kunbo.cn.app.entity.DeliveryNotice;
import com.kunbo.cn.app.service.DeliveryNoticeService;
import com.kunbo.cn.enumeration.core.logistics.CarState;
import com.kunbo.cn.enumeration.core.logistics.DriverState;

@Service
public class DeliveryNoticeImpl implements DeliveryNoticeService{
	
	@Autowired
	private DeliveryNoticeDao dao;
	@Autowired
	private JdbcTemplate jdbc;
	

	

	@Override
	public DeliveryNotice selNotice(Integer id) {
		// TODO Auto-generated method stub
		return dao.findOne(id);
	}

	@Override
	public List<Map<String, Object>> selAllNotice(String notificationNumber,Integer page,Integer pageSize,Integer wayBillUnit) {
		// TODO Auto-generated method stub
		String sql = "select * from business_delivery_notice_manage where 1 = 1";
		if(notificationNumber != null && !notificationNumber.equals("")) {
			sql += " and notice_code like '%" + notificationNumber + "%'";
		}
		String unit = "";
		String unitSql = "select name_of_transport_unit from transport_unit_management where id = " + wayBillUnit;
		if(jdbc.queryForList(unitSql) != null && jdbc.queryForList(unitSql).size()>0) {
			unit = jdbc.queryForList(unitSql).get(0).get("name_of_transport_unit").toString();
		}
		
		sql += " and trans_unit = '" + unit + "'";
		sql += " group by id desc limit " + (page*pageSize) + "," + pageSize;
		
		return jdbc.queryForList(sql);
	}

	@Override
	public Boolean saveNotice(String noticeCode, String waybillsCode, String orderCode,
			String supplier, String material, String address, String transport_unit, String car_number, String frid,
			String driver, String id_number, String note,float accruals,float vehicle_authenticated_load,String uuid,String create_user_id) {
		// TODO Auto-generated method stub
		DeliveryNotice notice = new DeliveryNotice();
		notice.setNoticeCode(noticeCode);
		notice.setWaybillCode(waybillsCode);
		notice.setOrderCode(orderCode);
		notice.setSupplier(supplier);
		notice.setMaterial(material);
		notice.setReceive_unit(address);
		notice.setTransUnit(transport_unit);
		notice.setTrainNumber(car_number);
		notice.setFrid(frid);
		notice.setDriver(driver);
		notice.setId_number(id_number);
		notice.setNote(note);
		notice.setCreate_user_id(create_user_id);
		notice.setUuid(uuid);
		notice.setState("7");
		notice.setAccruals(accruals);
		notice.setVehicle_authenticated_load(vehicle_authenticated_load);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		notice.setCreateDate(format.format(new Date()));
		notice.setCheck_state("0");
		notice = dao.save(notice);
		if(notice != null) {
			int carUpdate = jdbc.update("update vehicle_management set car_state = '" + CarState.getName(2) + "' where license_plate_number = '" + car_number + "'");
			int driverUpdate = jdbc.update("update driver_management set driver_state = '" +DriverState.getName(2)+ "' where idcrad = '" + id_number +"'");
		if(carUpdate > 0 && driverUpdate > 0 ) {
			return true;
		}
		}else {
			return false;
		}
	//String  sqlData = "insert into business_delivery_notice_manage(notice_code,waybill_code,order_code,supplier,material,address,trans_unit,train_number,frid,driver,id_number,note,accruals,vehicle_authenticated_load,create_date) Value ('"+noticeCode+"','"+waybillsCode+"','"+orderCode+"','"+supplier+"','"+material+"','"+address+"','"+transport_unit+"','"+car_number+"','"+frid+"','"+driver+"','"+id_number+"','"+note+"',"+accruals+","+vehicle_authenticated_load+",'"+format.format(new Date())+"')";
	//	jdbc.execute(sqlData);
		/*int i = 0;
		//notice = dao.save(notice);
			String sqls = "select purchase_waybill_stayfreightvolume from business_purchase_waybill where id = " + Integer.parseInt(waybillsCode);
			List<Map<String,Object>> list = jdbc.queryForList(sqls);
			if(list != null && list.size() >0) {
				if(list.get(0).get("purchase_waybill_stayfreightvolume") != null  && !list.get(0).get("purchase_waybill_stayfreightvolume").toString().isEmpty()) {
					double a = Double.parseDouble(list.get(0).get("purchase_waybill_stayfreightvolume").toString());
					double b = a - accruals;
					if(b < 0 || b == 0 ) {
						sqls = "update business_purchase_waybill set purchase_waybill_stayfreightvolume = 0 where id = " + + Integer.parseInt(waybillsCode);
					}else {
						sqls = "update business_purchase_waybill set purchase_waybill_stayfreightvolume = "+ b +" where id = " + + Integer.parseInt(waybillsCode);
					}
					i = jdbc.update(sqls);
			}
			}
			if(i > 0 ) {
				return true;
			}else {
				return false;
			}*/
		return false;
	}

	@Override
	public Boolean updNotice(Integer id, String notice_type, String noticeCode, String waybillsCode, String orderCode,
			String supplier, String material, String address, String transport_unit, String car_number, String frid,
			String driver, String id_number, String note,float accruals,float vehicle_authenticated_load) {
		// TODO Auto-generated method stub
		DeliveryNotice notice = dao.findOne(id);
		//String sql = "update business_delivery_notice_manage set notice_type = '" + notice_type +"',noticeCode='" + noticeCode +"',waybillCode='"+waybillsCode+"',orderCode='"+orderCode+"',supplier='"+supplier+"',material='"+material+"',address='" + address +"',transUnit='"+ transport_unit + "',trainNumber='"+car_number+"',frid='"+frid+"',driver='"+driver+"',id_number='"+id_number+"',note='"+note+"',accruals="+accruals+",vehicle_authenticated_load="+vehicle_authenticated_load + "where id = " + id;
		//int i = jdbc.update(sql);
		notice.setNotice_type(notice_type);
		notice.setNoticeCode(noticeCode);
		notice.setWaybillCode(waybillsCode);
		notice.setOrderCode(orderCode);
		notice.setSupplier(supplier);
		notice.setMaterial(material);
		notice.setReceive_unit(address);
		notice.setTransUnit(transport_unit);
		notice.setTrainNumber(car_number);
		notice.setFrid(frid);
		notice.setDriver(driver);
		notice.setId_number(id_number);
		notice.setNote(note);
		notice.setAccruals(accruals);
		notice.setVehicle_authenticated_load(vehicle_authenticated_load);
		notice.setState("7");
		notice = dao.save(notice);
		if(notice != null)
			return true;
		else
			return false;
	}


	@Override
	public List<Map<String, Object>> platformConfirmation(String tableName,String idCard) {
		// TODO Auto-generated method stub
		String sql = null;
		//采购磅单
		if(tableName != null) {
			if(tableName.equals("db_business_purchase_pound_list")) {
				sql = "select * from db_business_purchase_pound_list where idnumber = '" + idCard + "'";
				}else{
				sql = "select * from db_business_sales_pound_list where idnumber = '" + idCard + "'";
				}
			return jdbc.queryForList(sql);
		}
		System.out.println(sql);
		return null;
	}

	@Override
	public Boolean selCard(String idCard, Double primary, Double weightDeduction, Double buckle) {
		// TODO Auto-generated method stub
		String sql = "update db_business_purchase_pound_list set old_net_weight = " + weightDeduction + ",buckle_weight = " + buckle + ",buckle_miscellaneous=" + primary + " where idnumber = '" + idCard +"'";
		if(jdbc.update(sql) > 1)
			return true;
		else
			return false;
	}

}
